package com.arpgalaxy.spring.boot.autoconfigure.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;

import javax.servlet.Filter;
import java.util.*;

/**
 * @author arpgalaxy
 * @date 2021/4/11
 * @email 13605560342@163.com
 * @description
 */
@Configuration
@EnableConfigurationProperties(ArpgalaxyShiroProperties.class)
@Conditional({ArpgalaxyShiroCondition.class})
public class ArpgalaxyShiroAutoConfiguration {
    @Bean("sessionManager")
    @ConditionalOnMissingBean
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 是否定时检查session
        sessionManager.setSessionValidationSchedulerEnabled(false);
        return sessionManager;
    }
    @Bean("arpgalaxyShiroRealm")
    @ConditionalOnMissingBean
    public ArpgalaxyShiroRealm arpgalaxyShiroRealm(){
        return new ArpgalaxyShiroRealm();
    }

    @Bean("securityManager")
    @ConditionalOnMissingBean
    public SecurityManager securityManager(SessionManager sessionManager,ArpgalaxyShiroRealm[] arpgalaxyShiroRealms){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        if (arpgalaxyShiroRealms.length != 0){
            securityManager.setRealms(Arrays.asList(arpgalaxyShiroRealms));
        }
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }

//    @Bean
//    @ConditionalOnMissingBean
//    public ShiroAuthenticatingFilter shiroAuthenticatingFilter(){
//        return new ShiroAuthenticatingFilter();
//    }
    @Bean("shiroFilter")
    @ConditionalOnMissingBean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager,ArpgalaxyShiroProperties properties) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
//        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("shiroAuthenticatingFilter", new ShiroAuthenticatingFilter(properties));
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/**", "shiroAuthenticatingFilter");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    @ConditionalOnMissingBean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @ConditionalOnMissingBean

    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    @ConditionalOnMissingBean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
